Skip to content

Say a C# local's type when it has no initialiser to infer from [patch] - #356

Merged
matt-edmondson merged 1 commit into
mainfrom
fix/355-csharp-uninitialised-local
Oct 31, 2026
Merged

matt-edmondson merged 1 commit into
mainfrom
fix/355-csharp-uninitialised-local

Conversation

@matt-edmondson

Copy link
Copy Markdown
Contributor

Fixes #355

What changed

CSharpGenerator.GenerateVariableDeclaration wrote var whenever IsTypeInferred was set or Type was null, including when there was no initialiser. C# can only infer from an initialiser, so this produced var x;, which fails with CS0818.

The generator now writes:

  • var only when there is an initialiser to infer from
  • the declared type when there is no initialiser, even if "Inferred" is ticked (int typed;)
  • object with an inexpressible note when there is neither a type nor an initialiser (object untyped;). This matches the existing fallback for held variables.

A held (file/namespace-scope) variable that is inferred, has a declared type and has no initialiser now also gets its declared type, not object.

Tests

  • New CSharpGeneratedSourceCompilesTests.LocalsWithoutAnInitialiser_Compile compiles all three cases with dotnet build. I checked that it fails on main (the output was var untyped; var typed;) and passes with the fix.
  • Full Coder.Test suite: 1552 passed, 0 failed.

Generated by Claude Code

C# infers `var` only from an initialiser (CS0818), so a local declared with
no initial value now uses its declared type even when Inferred is ticked,
and falls back to `object` with a note when it has no type either. Locals
with an initialiser are still written as `var`.

Fixes #355

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017vfJW34ihN37UX2iCsueam
@sonarqubecloud

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

C#: a local with no initialiser and no declared type (or with "Inferred" ticked) is generated as var x;, which fails with CS0818

2 participants